1 using System;
2 using
UnityEngine;
3
4 namespace
ProceduralToolkit
5 {

6     ///
<summary>
7     ///
Texture extensions
8     ///
</summary>
9     
public static class TextureE
10     {

11         ///
<summary>
12         ///
Draws line on texture
13         ///
</summary>
14         
public static void DrawLine(this Texture2D texture, Vector2Int v0, Vector2Int v1, Color color)
15         {
16             
if (texture == null)
17             {
18                 
throw new ArgumentNullException("texture");
19             }
20             Draw.RasterLine(v0, v1, (x, y) => texture.SetPixel(x, y, color));
21         }

22
23         ///
<summary>
24         ///
Draws line on texture
25         ///
</summary>
26         
public static void DrawLine(this Texture2D texture, int x0, int y0, int x1, int y1, Color color)
27         {
28             
if (texture == null)
29             {
30                 
throw new ArgumentNullException("texture");
31             }
32             Draw.RasterLine(x0, y0, x1, y1, (x, y) => texture.SetPixel(x, y, color));
33         }

34
35         ///
<summary>
36         ///
Draws anti-aliased line on texture
37         ///
</summary>
38         
public static void DrawAALine(this Texture2D texture, Vector2Int v0, Vector2Int v1, Color color)
39         {
40             
if (texture == null)
41             {
42                 
throw new ArgumentNullException("texture");
43             }
44             Draw.RasterAALine(v0, v1,
45                 (x, y, t) => texture.SetPixel(x, y, Color.Lerp(texture.GetPixel(x, y), color, t)));
46         }

47
48         ///
<summary>
49         ///
Draws anti-aliased line on texture
50         ///
</summary>
51         
public static void DrawAALine(this Texture2D texture, int x0, int y0, int x1, int y1, Color color)
52         {
53             
if (texture == null)
54             {
55                 
throw new ArgumentNullException("texture");
56             }
57             Draw.RasterAALine(x0, y0, x1, y1,
58                 (x, y, t) => texture.SetPixel(x, y, Color.Lerp(texture.GetPixel(x, y), color, t)));
59         }

60
61         ///
<summary>
62         ///
Draws circle on texture
63         ///
</summary>
64         
public static void DrawCircle(this Texture2D texture, Vector2Int center, int radius, Color color)
65         {
66             
if (texture == null)
67             {
68                 
throw new ArgumentNullException("texture");
69             }
70             Draw.RasterCircle(center, radius, (x, y) => texture.SetPixel(x, y, color));
71         }

72
73         ///
<summary>
74         ///
Draws circle on texture
75         ///
</summary>
76         
public static void DrawCircle(this Texture2D texture, int centerX, int centerY, int radius, Color color)
77         {
78             
if (texture == null)
79             {
80                 
throw new ArgumentNullException("texture");
81             }
82             Draw.RasterCircle(centerX, centerY, radius, (x, y) => texture.SetPixel(x, y, color));
83         }

84
85         ///
<summary>
86         ///
Draws filled circle on texture using Bresenham's algorithm
87         ///
</summary>
88         
public static void DrawFilledCircle(this Texture2D texture, Vector2Int center, int radius, Color color)
89         {
90             
if (texture == null)
91             {
92                 
throw new ArgumentNullException("texture");
93             }
94             Draw.RasterFilledCircle(center, radius, (x, y) => texture.SetPixel(x, y, color));
95         }

96
97         ///
<summary>
98         ///
Draws filled circle on texture using Bresenham's algorithm
99         ///
</summary>
100         
public static void DrawFilledCircle(this Texture2D texture, int centerX, int centerY, int radius, Color color)
101         {
102             
if (texture == null)
103             {
104                 
throw new ArgumentNullException("texture");
105             }
106             Draw.RasterFilledCircle(centerX, centerY, radius, (x, y) => texture.SetPixel(x, y, color));
107         }

108
109         ///
<summary>
110         ///
Draws filled rectangle on texture
111         ///
</summary>
112         
public static void DrawRect(this Texture2D texture, int x, int y, int blockWidth, int blockHeight, Color color)
113         {
114             
if (texture == null)
115             {
116                 
throw new ArgumentNullException("texture");
117             }
118             
var colors = new Color[blockWidth*blockHeight];
119             
for (int _y = 0; _y < blockHeight; _y++)
120             {
121                 
for (int _x = 0; _x < blockWidth; _x++)
122                 {
123                     colors[_x + _y*blockWidth] = color;
124                 }
125             }
126             texture.SetPixels(x, y, blockWidth, blockHeight, colors);
127         }

128
129         ///
<summary>
130         ///
Fills texture with gradient
131         ///
</summary>
132         
public static void DrawGradient(this Texture2D texture, Gradient gradient, Directions progressionDirection)
133         {
134             
if (texture == null)
135             {
136                 
throw new ArgumentNullException("texture");
137             }
138             texture.DrawGradientRect(
0, 0, texture.width, texture.height, gradient, progressionDirection);
139         }

140
141         ///
<summary>
142         ///
Draws gradient rectangle on texture
143         ///
</summary>t
144         
public static void DrawGradientRect(this Texture2D texture, int x, int y, int blockWidth, int blockHeight,
145             Gradient gradient, Directions progressionDirection)
146         {
147             
if (texture == null)
148             {
149                 
throw new ArgumentNullException("texture");
150             }
151             Func<
int, int, Color> getColor;
152             
switch (progressionDirection)
153             {
154                 
case Directions.Left:
155                     getColor = (_x, _y) => gradient.Evaluate(
1 - (float) _x/(float) blockWidth);
156                     
break;
157                 
case Directions.Right:
158                     getColor = (_x, _y) => gradient.Evaluate((
float) _x/(float) blockWidth);
159                     
break;
160                 
case Directions.Down:
161                     getColor = (_x, _y) => gradient.Evaluate(
1 - (float) _y/(float) blockHeight);
162                     
break;
163                 
case Directions.Up:
164                     getColor = (_x, _y) => gradient.Evaluate((
float) _y/(float) blockHeight);
165                     
break;
166                 
default:
167                     
throw new ArgumentException("Not supported direction: " + progressionDirection,
168                         
"progressionDirection");
169             }
170
171             
var colors = new Color[blockWidth*blockHeight];
172             
for (int _y = 0; _y < blockHeight; _y++)
173             {
174                 
for (int _x = 0; _x < blockWidth; _x++)
175                 {
176                     colors[_x + _y*blockWidth] = getColor(_x, _y);
177                 }
178             }
179             texture.SetPixels(x, y, blockWidth, blockHeight, colors);
180         }

181
182         ///
<summary>
183         ///
Fills texture with white color
184         ///
</summary>
185         
public static void Clear(this Texture2D texture)
186         {
187             
if (texture == null)
188             {
189                 
throw new ArgumentNullException("texture");
190             }
191             
var pixels = new Color[texture.width*texture.height];
192             
for (var i = 0; i < pixels.Length; i++)
193             {
194                 pixels[i] = Color.white;
195             }
196             texture.SetPixels(pixels);
197         }

198
199         ///
<summary>
200         ///
Fills texture with specified color
201         ///
</summary>
202         
public static void Clear(this Texture2D texture, Color color)
203         {
204             
if (texture == null)
205             {
206                 
throw new ArgumentNullException("texture");
207             }
208             
var pixels = new Color[texture.width*texture.height];
209             
for (var i = 0; i < pixels.Length; i++)
210             {
211                 pixels[i] = color;
212             }
213             texture.SetPixels(pixels);
214         }

215
216         ///
<summary>
217         ///
Fills texture with specified color
218         ///
</summary>
219         
public static void Clear(this Texture2D texture, Color color, ref Color[] pixels)
220         {
221             
if (texture == null)
222             {
223                 
throw new ArgumentNullException("texture");
224             }
225             
if (pixels == null)
226             {
227                 
throw new ArgumentNullException("pixels");
228             }
229             
for (var i = 0; i < pixels.Length; i++)
230             {
231                 pixels[i] = color;
232             }
233             texture.SetPixels(pixels);
234         }
235     }
236 }


Gõ tìm kiếm nhanh...